package cn.bgs.controller;


import cn.bgs.common.R;
import cn.bgs.config.DirectXyConfig;
import cn.bgs.pojo.OfferAdvice;
import cn.bgs.service.OfferAdviceService;
import cn.bgs.util.OrderUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;

/**
 * <p>
 * 献言献策表 前端控制器
 * </p>
 *
 * @author Mr.Zhao
 * @since 2022-08-08
 */
@RestController
@RequestMapping("/offer-advice")
@Slf4j
public class OfferAdviceController {

    @Autowired
    private OfferAdviceService offerAdviceService;

    @Autowired
    private RabbitTemplate rabbitTemplate;


    @GetMapping("/offeradviceinfo")
    @ApiOperation(value = "献言献策信息查询")
    public R offeradviceinfo(@RequestParam Integer pageNum,
                               @RequestParam Integer pageSize,
                               @RequestParam(defaultValue = "") String offerPerson){
        QueryWrapper<OfferAdvice> queryWrapper= new QueryWrapper<>();
        queryWrapper.like("offer_person",offerPerson);
        queryWrapper.orderByDesc("create_time").eq("delete_flag",0);
        Page<OfferAdvice> page = offerAdviceService.page(new Page<>(pageNum, pageSize),queryWrapper);
        return R.ok().data("offeradviceinfo" , page);
    }



    @RequestMapping("/updatedInfo")
    @ApiOperation(value = "献言献策信息回复")
    public R updatedInfo(@RequestBody OfferAdvice offerAdvice){

        try {
            //发送消息到交换机
            //参数： 交换机、路由键、消息体
            rabbitTemplate.convertAndSend(DirectXyConfig.DIRECT_EXCHANGE_NAME, DirectXyConfig.DIRECT_ROUTING_KEY_NAME, offerAdvice);
            return R.ok();
        } catch (Exception e) {
            log.error(e.getMessage());
            return R.error();
        }
    }

    @RequestMapping("/deleteInfo/{id}")
    @ApiOperation(value = "献言献策信息删除")
    public R addInfo(@PathVariable Integer id){

        boolean save = offerAdviceService.removeById(id);
        if (save){
            return R.ok();
        }else {
            return R.error();
        }


    }


    @RequestMapping("/addInfo")
    @ApiOperation(value = "反馈信息添加")
    public R addInfo(@RequestBody OfferAdvice offerAdvice){
        LocalDateTime localDateTime = LocalDateTime.now();
        offerAdvice.setUserId(1);
        offerAdvice.setOfferAdNumber(OrderUtils.getDateTime());
        offerAdvice.setOfferTime(localDateTime);
        offerAdvice.setOfferState("待回复");
        offerAdvice.setDeleteFlag(0);
        offerAdvice.setCreateTime(localDateTime);
        offerAdvice.setUpdateTime(localDateTime);
        boolean save = offerAdviceService.save(offerAdvice);
        if (save){
            return R.ok();
        }else {
            return R.error();
        }


    }
}

